Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global TaskPool API improvements #10008

Merged
merged 5 commits into from
Oct 23, 2023

Conversation

BigWingBeat
Copy link
Contributor

Objective

Reduce code duplication and improve APIs of Bevy's global taskpools.

Solution

  • As all three of the global taskpools have identical implementations and only differ in their identifiers, this PR moves the implementation into a macro to reduce code duplication.
  • The init method is renamed to get_or_init to more accurately reflect what it really does.
  • Add a new try_get method that just returns None when the pool is uninitialized, to complement the other getter methods.
  • Minor documentation improvements to accompany the above changes.

Changelog

  • Added a new try_get method to the global TaskPools
  • The global TaskPools' init method has been renamed to get_or_init for clarity
  • Documentation improvements

Migration Guide

  • Uses of ComputeTaskPool::init, AsyncComputeTaskPool::init and IoTaskPool::init should be changed to ::get_or_init.

@BigWingBeat
Copy link
Contributor Author

BigWingBeat commented Oct 3, 2023

This PR was originally also going to fix #9477, but I couldn't figure out how best to implement it, and I figure it would probably be better to split that out from these other improvements anyhow.

@@ -20,7 +20,7 @@ pub fn heavy_compute(c: &mut Criterion) {
group.warm_up_time(std::time::Duration::from_millis(500));
group.measurement_time(std::time::Duration::from_secs(4));
group.bench_function("base", |b| {
ComputeTaskPool::init(TaskPool::default);
ComputeTaskPool::get_or_init(TaskPool::default);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Half of the calls are with the default value, maybe add a .get_or_init_default method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My intuition is that a lot of these calls could just be replaced with calls to the panicking getter, but you would have to look at the context of each call to be sure, which is probably out of the scope of this PR.

Regardless, I don't think that a default initializer should be added, as TaskPoolOptions from bevy_core is the preferred way of initializing the global taskpools. We should be encouraging using that, and discouraging using TaskPool::default.

TaskPoolOptions could potentially be moved from bevy_core to bevy_tasks to facilitate using that instead of TaskPool::default, but that is definitely outside of the scope of this PR.

@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Tasks Tools for parallel and async work labels Oct 5, 2023
@alice-i-cecile
Copy link
Member

alice-i-cecile commented Oct 6, 2023

Generally looks good. A bit reluctant to use macros for code deduplication purposes though. I'm not sure there's a better solution though: Deref might work, but a trait would probably regress the docs UX badly.

@hymm hymm self-requested a review October 10, 2023 23:44
@james7132 james7132 added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Oct 23, 2023
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Oct 23, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 23, 2023
Merged via the queue into bevyengine:main with commit faa1b57 Oct 23, 2023
26 checks passed
ameknite pushed a commit to ameknite/bevy that referenced this pull request Nov 6, 2023
# Objective

Reduce code duplication and improve APIs of Bevy's [global
taskpools](https://github.com/bevyengine/bevy/blob/main/crates/bevy_tasks/src/usages.rs).

## Solution

- As all three of the global taskpools have identical implementations
and only differ in their identifiers, this PR moves the implementation
into a macro to reduce code duplication.
- The `init` method is renamed to `get_or_init` to more accurately
reflect what it really does.
- Add a new `try_get` method that just returns `None` when the pool is
uninitialized, to complement the other getter methods.
- Minor documentation improvements to accompany the above changes.

---

## Changelog

- Added a new `try_get` method to the global TaskPools
- The global TaskPools' `init` method has been renamed to `get_or_init`
for clarity
- Documentation improvements

## Migration Guide

- Uses of `ComputeTaskPool::init`, `AsyncComputeTaskPool::init` and
`IoTaskPool::init` should be changed to `::get_or_init`.
@BigWingBeat BigWingBeat deleted the taskpool-api branch November 17, 2023 00:50
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
# Objective

Reduce code duplication and improve APIs of Bevy's [global
taskpools](https://github.com/bevyengine/bevy/blob/main/crates/bevy_tasks/src/usages.rs).

## Solution

- As all three of the global taskpools have identical implementations
and only differ in their identifiers, this PR moves the implementation
into a macro to reduce code duplication.
- The `init` method is renamed to `get_or_init` to more accurately
reflect what it really does.
- Add a new `try_get` method that just returns `None` when the pool is
uninitialized, to complement the other getter methods.
- Minor documentation improvements to accompany the above changes.

---

## Changelog

- Added a new `try_get` method to the global TaskPools
- The global TaskPools' `init` method has been renamed to `get_or_init`
for clarity
- Documentation improvements

## Migration Guide

- Uses of `ComputeTaskPool::init`, `AsyncComputeTaskPool::init` and
`IoTaskPool::init` should be changed to `::get_or_init`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Tasks Tools for parallel and async work C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants